package com.webwork.mood.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webwork.mood.system.dao.UserDao;
import com.webwork.mood.system.dataobject.Branch;
import com.webwork.mood.system.dataobject.Job;
import com.webwork.mood.system.dataobject.User;
import com.webwork.mood.system.service.BranchService;
import com.webwork.mood.system.service.JobService;
import com.webwork.mood.system.service.UserService;
import com.webwork.mood.util.Paging;
import com.webwork.mood.util.StrManager;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;
	
	@Autowired
	private JobService jobSve;
	
	@Autowired
	private BranchService branchSve;
	
	@Override
	public void insert(User user) {
		userDao.insert(user);
	}
	
	@Override
	public void update(User user) {
		userDao.update(user);
	}
	
	@Override
	public void save(User user) {
		if (this.get(user.getUserId()) != null) {
			this.update(user);
		} else {
			this.insert(user);
		}
	}
	
	@Override
	public void delete(String userId) {
		userDao.delete(userId);
	}
	
	@Override
	public List<User> query(User user) {
		return this.query(user, new Paging(0, 0));
	}
	
	@Override
	public List<User> query(User user, Paging page) {
		List<User> ret = userDao.query(user, page.getCurrPage(), page.getPageSize());
		for (User o : ret) {
			Job job = jobSve.get(o.getJobId());
			if (job != null) {
				o.setJobName(job.getJobName());
			}
			Branch branch = branchSve.get(o.getBranchId());
			if (branch != null) {
				o.setBranchName(branch.getBranchName());
			}
		}
		return ret;
	}
	
	@Override
	public User get(String userId) {
		if (StrManager.isEmpty(userId)) {
			return null;
		}
		User queryObj = new User();
		queryObj.setUserId(userId);
		List<User> user = this.query(queryObj);
		if (user != null && user.size() > 0) {
			if (user.size() > 1) {
				System.out.println("存在不止一条记录!");
			} else {
				return user.get(0);
			}
		} else {
			System.out.println("找不到该账号!");
		}
		return null;
	}

	@Override
	public int count(User user) {
		return userDao.count(user);
	}

}
